/*
 * All content copyright http://www.j2eefast.com, unless
 * otherwise indicated. All rights reserved.
 * No deletion without permission
 */
package com.j2eefast.web.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.j2eefast.web.entity.JumucClassifyEntity;
import com.j2eefast.web.mapper.JumucClassifyMapper;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.framework.utils.Constant;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Arrays;

/**
 * 【请填写功能名称】Service接口
 * @author: ZhouZhou
 * @date 2021-06-15 13:35:36
 */
@Service
public class JumucClassifyService extends ServiceImpl<JumucClassifyMapper,JumucClassifyEntity> {

	@Resource
	private JumucClassifyMapper jumucClassifyMapper;

	private final static String FORMAT_URL = "http://app.jumuc.com/jmGoodsClass/selGoodsClassAll";

	public List<JumucClassifyEntity> getThreeClassify(){
		JumucClassifyEntity query = new JumucClassifyEntity();
		query.setLevel(2);

		return jumucClassifyMapper.findList(query);
	}

	public void initClassify() {

		String baseUrl = "http://app.jumuc.com/jmGoodsClass/selGoodsClassAll";

		JSONObject data = doRequest(baseUrl);
		JSONArray classifyArr = data.getJSONArray("data");
		for (int i = 0; i < classifyArr.size(); i++) {
			JSONObject o = JSONUtil.parseObj(classifyArr.get(i));
			saveData(o);

		}


	}

	private void saveData(JSONObject o) {

		JumucClassifyEntity data = getData(o);
		saveData(data);

		JSONArray child = o.getJSONArray("child");
		if(child != null && child.size() > 0){
			for(int i = 0 ; i < child.size();i++){
				saveData(child.getJSONObject(i));
			}
		}
	}

	private JumucClassifyEntity getData(JSONObject o) {
		JumucClassifyEntity data = new JumucClassifyEntity();
		data.setClassName(o.getStr("className"));
		data.setId(o.getLong("id"));
		data.setLevel(o.getInt("level"));
		data.setParentId(o.getLong("parent_id"));

		return data;
	}

	private JSONObject doRequest(String url) {

		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("parentId","");
		String result = HttpRequest.post(url).header("Content-Type","application/json;charset=UTF-8")
				.body(JSONUtil.toJsonStr(paramMap)).execute().body();

		JSONObject data = JSONUtil.parseObj(result);

		return data;
	}

	private void saveData(JumucClassifyEntity classify) {
		JumucClassifyEntity exist = new JumucClassifyEntity();
		exist.setId(classify.getId());

		List<JumucClassifyEntity> dataList = jumucClassifyMapper.findList(exist);
		if(dataList != null && dataList.size() > 0){
			classify.setPkId(dataList.get(0).getPkId());
		}

		this.saveOrUpdate(classify);
	}

	/**
	 * mybaitis-plus 单表页面分页查询
     */
	public PageUtil findPage(Map<String, Object> params) {
		QueryWrapper<JumucClassifyEntity> queryWrapper = new QueryWrapper<JumucClassifyEntity>();
		String className = (String) params.get("className");
        queryWrapper.like(ToolUtil.isNotEmpty(className), "class_name", className);
		String level = (String) params.get("level");
        queryWrapper.eq(ToolUtil.isNotEmpty(level), "level", level);
		String parentId = (String) params.get("parentId");
        queryWrapper.eq(ToolUtil.isNotEmpty(parentId), "parent_id", parentId);
		Page<JumucClassifyEntity> page = jumucClassifyMapper.selectPage(new Query<JumucClassifyEntity>(params).getPage(), queryWrapper);
		return new PageUtil(page);
    }

	/**
	 * 自定义分页查询，含关联实体对像
	 */
	public PageUtil findPage(Map<String, Object> params,JumucClassifyEntity jumucClassifyEntity) {
		Page<JumucClassifyEntity> page = jumucClassifyMapper.findPage(new Query<JumucClassifyEntity>(params).getPage(),
					jumucClassifyEntity,
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	/**
	* 查列表
	*/
	public List<JumucClassifyEntity> findList(JumucClassifyEntity jumucClassifyEntity){
		return jumucClassifyMapper.findList(jumucClassifyEntity);
	}

	/**
     * 批量删除
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteBatchByIds(Long[] ids) {
		return this.removeByIds(Arrays.asList(ids));
	}

	/**
     * 单个删除
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean delJumucClassifyById(Long pkId) {
		return this.removeById(pkId);
	}

	/**
     * 保存
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean addJumucClassify(JumucClassifyEntity jumucClassify){
		if(this.save(jumucClassify)){
			return true;
		}
        return false;
    }

	/**
     * 修改根居ID
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean updateJumucClassifyById(JumucClassifyEntity jumucClassify) {
		if(this.updateById(jumucClassify)){
			return true;
		}
		return false;
	}

	/**
     * 根居ID获取对象
     */
	public JumucClassifyEntity findJumucClassifyById(Long pkId){
		return jumucClassifyMapper.findJumucClassifyById(pkId);
	}


}
